package de.mmis.core.base.informationprovider;

import java.util.Map;

import de.mmis.core.base.Position;
import de.mmis.core.base.Type;
import de.mmis.core.base.abstracttree.Tree;

/**
 * basic {@link DeviceInformationProvider} implementation propagating other
 * requests to its parent provider if present.
 * 
 * @author Christian Koch
 * 
 */
public class PropagatingDeviceInformationProvider implements
		DeviceInformationProvider {

	private final DeviceInformationProvider parent;

	private final ClassLoader classLoader;

	public PropagatingDeviceInformationProvider(DeviceInformationProvider parent) {
		this(parent, PropagatingDeviceInformationProvider.class
				.getClassLoader());
	}

	public PropagatingDeviceInformationProvider(
			DeviceInformationProvider parent, ClassLoader classLoader) {
		this.parent = parent;
		this.classLoader = classLoader;
	}

	@Override
	public Class<?>[] getControlComponentClasses() {
		if (this.parent != null)
			return this.parent.getControlComponentClasses();
		return null;
	}

	@Override
	public Class<?>[] getRepresentationComponentClasses() {
		if (this.parent != null)
			return this.parent.getRepresentationComponentClasses();
		return null;
	}

	@Override
	public Map<String, String> getDisplayName(String id) {
		if (this.parent != null)
			return this.parent.getDisplayName(id);
		return null;
	}

	@Override
	public Type[] getInterfaces(Type deviceType) {
		if (this.parent != null)
			return this.parent.getInterfaces(deviceType);
		return null;
	}

	@Override
	public Tree getLocalInstanceTree(String id) {
		if (this.parent != null)
			return this.parent.getLocalInstanceTree(id);
		return null;
	}

	@Override
	public Map<String, Position> getPosition(String id) {
		if (this.parent != null)
			return this.parent.getPosition(id);
		return null;
	}

	@Override
	public Map<String, Object> getPropertiesMap(String id) {
		if (this.parent != null)
			return this.parent.getPropertiesMap(id);
		return null;
	}

	@Override
	public Type getDeviceType(String id) {
		// ask parent first
		Type ret = null;
		if (this.parent != null) {
			ret = this.parent.getDeviceType(id);
			if (ret != null)
				return ret;
		}
		return null;
	}

	@Override
	public ClassLoader getInformationProviderClassLoader() {
		if (this.parent != null)
			return this.parent.getInformationProviderClassLoader();
		return this.classLoader;
	}
}
